let App = getApp();
import Toast from '@vant/weapp/toast/toast';
// 获取当前时间
let currenTime = new Date();
let getData = function (_time) {
  let time = new Date(_time)
  let _year = time.getFullYear();
  let _month = time.getMonth() + 1;
  let _date = time.getDate();
  // console.log(_year, _month, _date);
  if (_month < 10) _month = '0' + _month;
  if (_date < 10) _date = '0' + _date;
  return _year + '-' + _month + '-' + _date;
}
// 获取昨天日期
let beforeDate = function () {
  let now = currenTime;
  let _time = new Date(now.getTime() - 24 * 60 * 60 * 1000);
  let _year = _time.getFullYear();
  let _month = _time.getMonth() + 1;
  let _date = _time.getDate();
  if (_month < 10) _month = '0' + _month;
  if (_date < 10) _date = '0' + _date;
  return _year + '-' + _month + '-' + _date;
}
// 获取当前时间的上一周日期
let weekDate = function () {
  let now = currenTime,
    beginTime = "",
    endTime = "",
    lastWeek = {};
  var weekDate = new Date(now.getTime() - 7 * 24 * 3600 * 1000); // 计算开始时间用
  var weekDate2 = new Date(now.getTime() - 7 * 24 * 3600 * 1000); // 计算结束时间用

  var day = weekDate.getDay();
  var time = weekDate.getDate() - day + (day === 0 ? -6 : 1);

  var startDate = new Date(weekDate.setDate(time));
  var endDate = new Date(weekDate2.setDate(time + 6));
  let startMonth = startDate.getMonth() + 1,
    startDay = startDate.getDate(),
    endMonth = endDate.getMonth() + 1,
    endDay = endDate.getDate();
  if (startMonth < 10) startMonth = '0' + startMonth;
  if (startDay < 10) startDay = '0' + startDay;
  if (endMonth < 10) endMonth = '0' + endMonth;
  if (endDay < 10) endDay = '0' + endDay;

  beginTime = startDate.getFullYear() + '-' + startMonth + '-' + startDay;
  endTime = endDate.getFullYear() + '-' + endMonth + '-' + endDay;
  lastWeek.start = beginTime;
  lastWeek.end = endTime;
  lastWeek.weekth = getCurrenWeek(beginTime);
  // console.log('是的',lastWeek);
  return lastWeek;
}
// 计算时间为当年的第几周
let getCurrenWeek = function (_date) {
  // console.log(777,_date)
  let time, week, checkDate = new Date(new Date(_date));
  checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
  time = checkDate.getTime();
  checkDate.setMonth(0);
  checkDate.setDate(1);
  week = Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
  if (week < 10) {
    week = '0' + week;
  }
  // console.log('周',checkDate.getFullYear()+ week.toString());
  return checkDate.getFullYear() + week.toString();
}
// 获取某月的第一天和最后一天
let getDateByMonth = function (timeStamp) {
  let inDate = new Date(timeStamp)
  let year = inDate.getFullYear()
  let month = inDate.getMonth()
  let startTime = new Date(year, month, 1).getTime()
  let endTime = new Date(year, month + 1, 0).getTime()
  // console.log(getData(startTime),getData(endTime))
  return {
    startTime: getData(startTime),
    endTime: getData(endTime)
  }
}
// token过期重新登录
let reLogin = function () {
  wx.removeStorageSync('currentOrganize');
  wx.removeStorageSync('token');
  wx.removeStorageSync('initInfo');
  wx.reLaunch({
    url: '/pages/login/index'
  })
  // wx.login({
  //   success: res => {
  //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
  //     wx.request({
  //       url: App.globalData.testApi + '/wechat/passport/login',
  //       method: "POST",
  //       header: {
  //         "Content-Type": "application/x-www-form-urlencoded"
  //       },
  //       data: {
  //         code: res.code,
  //         // mobile: 17671621513,
  //         // sms_code: 1234
  //       },
  //       success(resapi) {
  //         if (resapi.data.code == 0) {
  //           wx.setStorageSync('token', resapi.data.data.token);
  //           getInit(resapi.data.data.token);
  //           // Toast("登录成功，即将进入首页！")
  //         } else {
  //           Toast(resapi.data.message)
  //         }
  //       },
  //       fail(resapi) {
  //         console.log('失败', resapi);
  //       },
  //       complete(resapi) {

  //       }
  //     })
  //   },
  //   fail: res => {

  //   }
  // })
}
let getInit = function (_token, _type) {
  wx.request({
    url: App.globalData.testApi + '/wechat/passport/init',
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
      "token": _token
    },
    data: {

    },
    success(resapi) {
      if (resapi.data.code == 0) {
        wx.setStorageSync('initInfo', JSON.stringify(resapi.data.data));
        let _obj = resapi.data.data,
          _id = 0,
          _info = {};
        if(_obj.extra.length == 0){
          wx.reLaunch({
            url: '/pages/noPage/index'
          })
          return;
        }
        if (_obj.last_organization_id > 0) {
          _id = _obj.last_organization_id;
        } else {
          _id = _obj.extra[0].organization_id;
        }
        // console.log(111,_obj,_id)
        for (let i = 0; i < _obj.extra.length; i++) {
          if (_id == _obj.extra[i].organization_id) {
            _info = _obj.extra[i];
          }
        }
        if (Object.keys(_info).length == 0) {
          _info = _obj.extra[0];
        }
        wx.setStorageSync('currentOrganize', JSON.stringify(_info));
        if (_type == 'login') {
          setTimeout(() => {
            wx.redirectTo({
              url: "/pages/index/index"
            })
          }, 1000)
        } else {
          let _page = getCurrentPages();
          // console.log('page',_page)
          _page[0].onShow();
        }
      } else {
        Toast(resapi.data.message)
      }
    },
    fail(resapi) {
      console.log('失败', resapi);
    },
    complete(resapi) {
      wx.hideLoading();
    }
  })
}
// 跳转品种详情
let toDetail = function (id) {
  wx.navigateTo({
    url: '/pages/publish/detail/index?id=' + id
  })
}
module.exports = {
  currenTime,
  getData,
  beforeDate,
  weekDate,
  getCurrenWeek,
  getDateByMonth,
  reLogin,
  getInit,
  toDetail
}